package com.atguigu.leetcode;
import cn.hutool.core.lang.Console;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
 * https://leetcode-cn.com/problems/add-two-numbers/
 */
public class _2AddTwoNumbers {
    ListNode node3 = null;
    ListNode node6= null;
    @BeforeEach
    public void init(){
        ListNode node1 = new ListNode(2);
        ListNode node2 = new ListNode(4, node1);
        node3 = new ListNode(3, node2);
        ListNode node4 = new ListNode(5);
        ListNode node5 = new ListNode(6, node4);
        node6 = new ListNode(4, node5);
    }

    @Test
    public void my() {
        ListNode listNode = addTwoNumbers(node3, node6);
        Console.log(listNode);
    }

    @Test
    public void leetCode(){
        ListNode listNode = addTwoNumbersByLeetCode(node3, node6);
        Console.log(listNode);
    }

    public ListNode addTwoNumbersByLeetCode(ListNode l1, ListNode l2) {
        ListNode head = null, tail = null;
        int carry = 0;
        while (l1 != null || l2 != null) {
            int n1 = l1 != null ? l1.val : 0;
            int n2 = l2 != null ? l2.val : 0;
            int sum = n1 + n2 + carry;
            if (head == null) {
                head = tail = new ListNode(sum % 10);
            } else {
                tail.next = new ListNode(sum % 10);
                tail = tail.next;
            }
            carry = sum / 10;
            if (l1 != null) {
                l1 = l1.next;
            }
            if (l2 != null) {
                l2 = l2.next;
            }
        }
        if (carry > 0) {
            tail.next = new ListNode(carry);
        }
        return head;
    }

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        Integer h1 = getNumber(l1);
        Integer h2 = getNumber(l2);
        int i = h1 + h2;
        return getListNode(i);
    }

    private Integer getNumber(ListNode l1) {
        int n = 0;
        int num = 0;
        while (l1 != null) {
            if (n == 0) {
                num = l1.val;
            } else {
                num += l1.val * Math.pow(10, n);
            }
            l1 = l1.next;
            n++;
        }
        return num;
    }

    private ListNode getListNode(int i) {
        ListNode first = null;
        ListNode pre = null;
        while (i / 10 != 0) {
            int num = i % 10;
            ListNode node = new ListNode();
            node.val = num;
            if (pre != null) {
                pre.next = node;
            }
            if (first == null) {
                first = node;
            }
            pre = node;
            i = i / 10;
        }
        int num = i % 10;
        ListNode node = new ListNode();
        node.val = num;
        pre.next = node;
        return first;
    }

    static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}